/*
 * ws2812.c
 *
 *  Created on: Jun 22, 2020
 *      Author: admin
 */

#include "ws2812.h"
#include <string.h>

HAL_LED_PWM_BufferTypedef PWM_sendBuffer={0};

void WS2812_SetPixelBuffer(uint8_t* pixelBuffer)
{
	for(uint8_t i = 0; i < PIXEL_NUM; i++)
	{
		for(uint8_t j = 0; j < 8; j++)
		{
			PWM_sendBuffer.data[24 * i + j] = pixelBuffer[i * 3] & (0x80 >> j) ? BIT_1 : BIT_0;
			PWM_sendBuffer.data[24 * i + j + 8] = pixelBuffer[i * 3 + 1] & (0x80 >> j) ? BIT_1 : BIT_0;
			PWM_sendBuffer.data[24 * i + j + 16] = pixelBuffer[i * 3 + 2] & (0x80 >> j) ? BIT_1 : BIT_0;
		}
	}
}

void WS2812_SendData(void)
{
	HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1, (uint32_t*)&PWM_sendBuffer, 24 * PIXEL_NUM+4);
}

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance == TIM3)
	{
		HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
	}
}
